<?php

use kartik\widgets\TouchSpin;
use unclead\multipleinput\MultipleInput;
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use kzo\base\models\statics\EntityModelStatus;
use kzo\base\widgets\ui\bs4\Alert;
use yii\helpers\Url;
use yii\widgets\Pjax;

/* @var $model \backend\models\c2\form\SkuAssembleForm */

$regularLangName = \Yii::$app->kzoHelper->getRegularLangName();
$messageName = $model->getMessageName();
?>

<?php //Pjax::begin([
//     'id' => $model->getDetailPjaxName(),
//     // 'options' => ['style' => ['padding' => '10px']],
//     'formSelector' => $model->getBaseFormName(true),
//     'enablePushState' => false,
//     'clientOptions' => [
//         'skipOuterContainers' => true
//     ]
// ]) ?>


<?php
$form = ActiveForm::begin([
    'action' => [
        'assemble/sku-assemble-edit',
        'id' => $model->entityModel->id,
        // 'type' => \common\models\c2\statics\AssembleType::TYPE_MATERIAL
    ],
    'options' => [
        'id' => $model->getBaseFormName(),
        'data-pjax' => true,
    ]]);
?>

<?php if (Yii::$app->session->hasFlash($messageName)): ?>
    <?php if (!$model->hasErrors()) {
        echo Alert::widget([
            'type' => Alert::TYPE_SUCCESS,
            'title' => 'Well Down',
            'icon' => 'fas fa-ok-circle',
            'body' => Yii::$app->session->getFlash($messageName),
            'showSeparator' => true,
            'delay' => 2000
        ]);
    } else {
        echo Alert::widget([
            'type' => Alert::TYPE_WARNING,
            'title' => 'Warning!',
            'icon' => 'fas fa-ok-circle',
            'body' => Yii::$app->session->getFlash($messageName),
            'showSeparator' => true,
        ]);
    }
    ?>
<?php endif; ?>

<?php

echo Form::widget([
    'model' => $model,
    'form' => $form,
    'columns' => 4,
    'attributes' => [
        'type' => [
            'type' => Form::INPUT_HIDDEN,
            'options' => [
                'value' => \common\models\c2\statics\AssembleType::TYPE_MATERIAL,
            ]
        ],
    ]
]);

$multipleItemsId = $model->getPrefixName('items');

echo Form::widget([
    'model' => $model,
    'form' => $form,
    'columns' => 1,
    'attributes' => [
        'items' => [
            'type' => Form::INPUT_WIDGET,
            'widgetClass' => unclead\multipleinput\MultipleInput::className(),
            'options' => [
                'addButtonOptions' => [
                    'class' => 'btn btn-success',
                    'label' => Html::tag('i', null, ['class' => 'fa fa-plus'])
                ],
                'removeButtonOptions' => [
                    'class' => 'btn btn-danger',
                    'label' => Html::tag('i', null, ['class' => 'fa fa-minus'])
                ],
                'data' => $model->items,
                // 'max' => 4,
                'allowEmptyList' => true,
                'rowOptions' => function ($model, $index, $context) {
                    return ['data-id' => $model['id']];
                },
                'columns' => [
                    [
                        'name' => 'id',
                        'type' => 'hiddenInput',
                    ],
                    [
                        'name' => 'product_id',
                        'type' => \kartik\select2\Select2::className(),
                        'title' => Yii::t('app.c2', 'Material'),
                        'enableError' => true,
                        'options' => [
                            // 'pluginOptions' => ['width' => '200px'],
                            'data' => ['' => 'selecting...'] + \common\models\c2\entity\Assemble::getHashMap('id', 'name', [
                                'type' => \common\models\c2\statics\ProductType::TYPE_ASSEMBLE,
                                'status' => EntityModelStatus::STATUS_ACTIVE,
                            ]),
                        ]
                    ],
                    [
                        'name' => 'num',
                        'title' => Yii::t('app.c2', 'Need Num'),
                        'enableError' => true,
                        'defaultValue' => 0,
                        'options' => [
                            'type' => 'number',
                            'class' => 'input-priority'
                        ]
                    ],
                    [
                        'name' => 'position',
                        'type' => kartik\widgets\TouchSpin::className(),
                        'title' => Yii::t('app.c2', 'Position'),
                        'defaultValue' => 50,
                    ],
                ]
            ],
        ],
    ]
]);


echo Html::submitButton('<i class="fa fa-save"></i> ' . Yii::t('app.c2', 'Save'), ['type' => 'button', 'class' => 'btn btn-primary float-right ml-2']);
echo Html::a('<i class="fa fa-window-close"></i> ' . Yii::t('app.c2', 'Close'), ['default/index'], ['data-pjax' => '0', 'data-dismiss' => 'modal', 'class' => 'btn btn-light float-right', 'title' => Yii::t('app.c2', 'Go Back'),]);

?>

<div style="clear: both"></div>
<?php ActiveForm::end(); ?>

<?php //Pjax::end() ?>


<?php
$js = "";
$js .= "jQuery('.btn.multiple-input-list__btn.js-input-remove').off('click').on('click', function(){
    var itemId = $(this).closest('tr').data('id');
    if(itemId){
       $.ajax({url:'" . Url::toRoute('assemble/delete-subitem') . "',data:{id:itemId}}).done(function(result){;}).fail(function(result){alert(result);});
    }
});\n";
$this->registerJs($js);
?>
